//配置参数
var option = {
	curpage :1,
	pagesize :5,
	pageTotal :''
}
//获取数据
getList();
function getList(){
	var html = [];
	var url = 'http://localhost/wbc-14/api/api.php/users?transform=1&order=id,desc&page='+option.curpage+','+option.pagesize;
	console.log(url)
	$.get(url,function(response){  //获取用户列表

		 option.pageTotal = response._results;// 分页  得到分页总数
		console.log(response.users)
		for( var i = 0; i < response.users.length; i++){
			html.push('<tr><td><input type="checkbox"></td>'+
				'<td>'+(i+1)+'</td>'+
				'<td>'+response.users[i].name+'</td>'+
				'<td>'+response.users[i].username+'</td>'+
				'<td>'+response.users[i].age+'</td>'+
				'<td>'+response.users[i].gender+'</td>'+
				'<td>'+response.users[i].hobbies+'</td>'+
				'</tr>')
		}
		$('#usersList tbody').html(html.join(''));
	})
}

//获取分页数、
var num;
function getpageSize(){
	 num =  Math.ceil(option.pageTotal / option.pageSize);
    var arr = new Array(num).fill(0), i;
    var html = [];
    if(option.curPage == 1) {
        html.push('<li class="prevPag disabled">')
    } else {
        html.push('<li class="prevPag">')
    }
    html.push('<a href="javascript:;" aria-label="Previous">'+
                    '<span aria-hidden="true">&laquo;</span>'+
                '</a>'+
            '</li>')
    
    for(i = 0; i < num; i++) {
        if((i+1) == option.curPage) {
            html.push('<li class="active jumpPag"><a href="javascript:;">'+(i+1)+'</a></li>')
        } else {
            html.push('<li class="jumpPag"><a href="javascript:;">'+(i+1)+'</a></li>')
        }
        
    }
    if(option.curPage == num) {
        html.push('<li class="nextPag disabled">')
    } else {
        html.push('<li class="nextPag">')
    }
    html.push(  '<a href="javascript:;" aria-label="Next">'+
                    '<span aria-hidden="true">&raquo;</span>'+
                '</a>'+
            '</li>')
            
    $('.pagination').html(html.join(''));
}

//添加按钮
$('#addUser').on('click',function(){addUser();})

//模态框保存按钮
$('#saveBtn').on('click',function(){saveUser();})




function addUser(){
	$('#userBox').modal('show');
}

function saveUser(){
    var data = {
        username: $('#username').val(),
        name: $('#user').val(),
        age: $('#age').val(),
        gender: '男',
        hobbies: '爬山'
    }
    var url = 'http://localhost/wbc-14/api/api.php/users';
    $.post(url, data, function(response) {
        $('#userBox').modal('hide');
        getList();
    })
}
